package cn.cvs.servlet.sysUser;

import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DelServlet", urlPatterns = "/sys/user/del")
public class DelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        int id = request.getParameter ( "id" ) != null ? Integer.parseInt ( request.getParameter ( "id" ) ) : 0;
        Map map=new HashMap<> (  );
        if (sysUserService.getUser ( id )!=null){
            List<Integer> ids = new ArrayList<> ( );
            ids.add ( id );
            int row = sysUserService.deleteUser ( ids );
            if (row > 0) {
                map.put ( "delResult","true" );
            }else {
                map.put ( "delResult","false" );
            }
        }else {
            map.put ( "delResult","notexist"  );
        }
        PrintWriter writer = response.getWriter ( );
        writer.println ( JSON.toJSONString ( map ) );
        writer.close ();
        return;
    }
}
